﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using ZoneFiveSoftware.Common.Data.Measurement;
using ZoneFiveSoftware.Common.Visuals;
using STCommon.Data;
using DistributionPlugin.Controller;
using STCommon.Controller;

namespace DistributionPlugin.View
{
    class ReportsLabelUnitProvider : ILabelUnitProvider
    {
        public ReportsLabelUnitProvider(DistributionReportsControl reportsControl, int reportPanelIndex)
        {
            Debug.Assert(reportsControl != null);
            Debug.Assert(reportPanelIndex >= 0 &&
                         reportPanelIndex < ReportPageOptions.MaxPanelCount);

            m_ReportsControl = reportsControl;
            m_OptionsInstance = ReportPageOptions.Instance.GetPanelOptions(reportPanelIndex);
        }

#region ILabelUnitProvider Members

        public Enums.SportTracksDataTrackTypes DataType
        {
            get { return m_OptionsInstance.SelectedDataType; }
        }


        public Length.Units DistanceUnit
        {
            get
            {
                Length.Units distanceUnit;
                Speed.Units speedUnit;

                Utils.GetCommonDistanceUnitForActivities(m_ReportsControl.ReportActivities, PluginMain.GetApplication(),
                                                         out distanceUnit, out speedUnit);

                return distanceUnit;
            }
        }

        public Speed.Units SpeedUnit
        {
            get
            {
                Length.Units distanceUnit;
                Speed.Units speedUnit;

                Utils.GetCommonDistanceUnitForActivities(m_ReportsControl.ReportActivities, PluginMain.GetApplication(),
                                                         out distanceUnit, out speedUnit);

                return speedUnit;
            }
        }

        public String GetUnitsTextForSelection()
        {
            switch (m_OptionsInstance.SelectedDataType)
            {
                case Enums.SportTracksDataTrackTypes.Cadence:
                    {
                        return CommonResources.Text.LabelRPM;
                    }
                case Enums.SportTracksDataTrackTypes.Elevation:
                    {
                        return Length.LabelPlural(Utils.GetCommonElevationUnitForActivities(m_ReportsControl.ReportActivities,
                                                                                            PluginMain.GetApplication()));
                    }
                case Enums.SportTracksDataTrackTypes.Grade:
                    {
                        return CommonResources.Text.LabelPercent;
                    }
                case Enums.SportTracksDataTrackTypes.HeartRate:
                    {
                        return String.Empty;
                    }
                case Enums.SportTracksDataTrackTypes.Speed:
                    {
                        return Speed.Label(SpeedUnit, new Length(1, DistanceUnit));
                    }
                case Enums.SportTracksDataTrackTypes.Power:
                    {
                        return CommonResources.Text.LabelWatts;
                    }
                default:
                    {
                        return String.Empty;
                    }
            }
        }

        public string GetSpeedLabelForSelection()
        {
            if (SpeedUnit == Speed.Units.Speed)
            {
                return CommonResources.Text.LabelSpeed;
            }
            else
            {
                return CommonResources.Text.LabelPace;
            }
        }

        public string GetIntervalTextForSelection()
        {
            if (m_OptionsInstance.SelectedDataType == Enums.SportTracksDataTrackTypes.Speed &&
                SpeedUnit == Speed.Units.Pace)
            {
                return Utils.TimeMinutesToString(m_OptionsInstance.SelectedDataTypeIntervalLength);
            }
            else
            {
                return String.Format("{0:0.##}", Math.Round(m_OptionsInstance.SelectedDataTypeIntervalLength, 2));
            }
        }

#endregion

        private DistributionReportsControl m_ReportsControl = null;
        private DistributionOptions m_OptionsInstance = null;
    }
}
